package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaomi.domain.Orders;
import com.xiaomi.domain.Phones;
import com.xiaomi.service.OrdersService;
import com.xiaomi.service.PhonesService;
import com.xiaomi.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author： YongZ
 * @date： 2024/6/4
 * @description：
 * @modifiedBy：
 */
@RestController
@Slf4j
public class PhoneController {
    @Autowired
    private PhonesService phonesService;
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/phones")
    public List<Phones> getAllPhones() {
        return phonesService.getAllPhones();
    }

    // post接口,添加
    @PostMapping("/shop/buy/add")
    public JsonResult add(@RequestBody Orders orders) {
        ordersService.save(orders);
        log.info("订单创建成功");
        // 返回
        return JsonResult.success("订单创建成功");
        //return orders.toString();
    }

    //phone_version
    @PostMapping("/phone_version")
    public JSONObject getPhoneVersion(@RequestParam("product_id") Long product_id) {
        Phones phoneInfo = phonesService.getById(product_id);
        JSONObject jsonObject = JSON.parseObject(phoneInfo.getVersion());
        return jsonObject;
    }
}
